<template>
  <div class="home-hot">
    <HomePanel title="人气推荐" sub-title="人气爆款 不容错过">
      <div style="position: relative;height: 426px;" ref="hotRef">
        <!-- 动画 -->
        <Transition name="fade" mode="out-in">
          <ul class="goods-list" v-if="goods && goods.length">
            <li v-for="item in goods" :key="item.id">
              <RouterLink :to="`/product/${item.id}`">
                <img :src="item.picture" alt="">
                <p class="name">{{ item.title }}</p>
                <p class="desc">{{ item.alt }}</p>
              </RouterLink>
            </li>
          </ul>
          <!-- 骨架屏 -->
          <HomeSkeleton bg="#f0f9f4" v-else></HomeSkeleton>
        </Transition>
      </div>
    </HomePanel>
  </div>
</template>

<script setup>
import { findHot } from '@/api/home'
import { ref } from 'vue'
import HomePanel from '../com/home-panel.vue'
import HomeSkeleton from '../com/home-skeleton.vue'
// 导入数据懒加载函数
import { useLazyData } from '@/hook'
const goods = ref([])
const hotRef = ref(null)
useLazyData(hotRef, () => {
  findHot().then(({ result }) => {
    goods.value = result
    console.log(result)
  })
})

</script>

<style lang="less" scoped>
.fade {
  &-leave {
    &-active {
      transition: opacity .5s .2s;
      // 两个动画同事触发，导致布局出现问题
      // 方式一： 设置position: absolute让骨架屏离开动画在最上面
      // position: absolute; //骨架离开动画在上一层的触发
      // width: 100%;
      // z-index: 1;
      // 方式二：给Transition设置动画模式，只处理离开的动画
    }

    &-to {
      opacity: 0;
    }
  }
}

.goods-list {
  display: flex;
  justify-content: space-between;
  height: 426px;

  li {
    width: 306px;
    height: 406px;
    .hoverShadow();

    img {
      width: 306px;
      height: 306px;
    }

    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }

    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
</style>